//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 鹏 周 on 15/12/13.
//  Copyright © 2015年 ycit. All rights reserved.
//

import UIKit
//定义协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //声明协议方法
    //用户将要登陆
    //optional 是OC中的关键字
    optional func userWillLogin()
    //用户将要注册
    func userWillRegister()
    
}

class VisitorLoginView: UIView {
    //声明代理  代理 要使用 weak 属性关键字
    //swift 属性默认都是strong  代理同样也需要使用weak
    weak var visitroViewDelegate: VisitorLoginViewDelegate?
    
    
    func setupInfo(tipText: String, imageName: String?){
    tipLabel.text = tipText
        if let name = imageName {
            circleView.image = UIImage(named: name)
            
            largeIcon.hidden = true
            
            bringSubviewToFront(circleView)
        } else {
        //就是首页
        //就开始做动画
        startAnimation()
        
        }
    }
    
    private func startAnimation() {
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 10.0
        anim.toValue = M_PI*2
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeIcon)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        for subView in subviews {
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置背景视图的约束 可视化的布局语言
        //OC 中可选项 一般使用 按位 或枚举选项  swift中直接指定数组
        //metrics:约束数值字典 [String: 数值],
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn":registerBtn]))
        
        
        //设置视图的背景颜色
        //UIColor(white: <#T##CGFloat#>, alpha: <#T##CGFloat#>) 设置灰度颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        //添加点击事件
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
        
//        startAnimation()
    }
    
    
   
    
    
    
    
    
    //MARK: 按钮的监听事件
    @objc private func registerBtnDidClick() {
        //代理调用协议方法  如果为 nil 就不会执行方法
        visitroViewDelegate?.userWillRegister()
    }
    
    @objc private func loginBtnDidClick() {
        
        //判断代理是否为空 && 是否响应方法  userWillLogin? 表示的是判断是否可以响应方法
        visitroViewDelegate?.userWillLogin?()
    }
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var largeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    private lazy var tipLabel: UILabel = {
    let l = UILabel()
        l.text = "sb"
        l.textAlignment = .Center
        l.textColor = UIColor.darkGrayColor()
        
        return l
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
         btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.sizeToFit()
        return btn
    }()

    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.sizeToFit()
        return btn
    }()

}
